﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Nexus.IPConfiguration.Management.Wrappers;
using Nexus.IPConfiguration.Management;
using Nexus.IPConfiguration.Configurator.NetworkAdapterConfigs;

namespace Nexus.IPConfiguration.Configurator
{
    internal static class ConfigChanger
    {
        #region fields
        private static NetworkAdapterFactory factory;
        #endregion

        #region constructors
        static ConfigChanger()
        {
            factory = new NetworkAdapterFactory();
        }
        #endregion

        internal static string EncodeParams(NetworkAdapterConfiguration config, string adapterCaption)
        {
            string args = string.Format("\"{0}\" {1} ", adapterCaption, config.IsDHCPEnabled);
            if (config.IsDHCPEnabled)
            {
                args += "# # # ";
            }
            else
            {
                args += string.Format("{0} {1} {2} ", config.IPAddress, config.SubnetMask, config.DefaultGateway);
            }

            args += string.Format("{0} ", config.ObtainDNSAutomatically);
            if (config.ObtainDNSAutomatically)
            {
                args += "# # ";
            }
            else
            {
                args += string.Format("{0} {1} ", string.IsNullOrWhiteSpace(config.DNS1) ? "#" : config.DNS1, string.IsNullOrWhiteSpace(config.DNS2) ? "#" : config.DNS2);
            }

            args += string.Format("{0} ", config.UseProxy);
            if (config.UseProxy)
            {
                args += string.Format("{0} {1} \"{2}\"", config.ProxyAddress, config.ProxyPort, config.ProxyOverride);
            }
            else
            {
                args += "# # #";
            }
            return args;
        }

        internal static void DecodeParams(string[] args, out string adapterName, out IPConfig ipConfig, out DNS dns, out ProxyConfig proxyConfig)
        {
            adapterName = args[0];

            bool isDHCPEnabled = bool.Parse(args[1]);
            ipConfig = new IPConfig();
            ipConfig.IsDHCPEnabled = isDHCPEnabled;
            if (ipConfig.IsDHCPEnabled == false)
            {
                ipConfig.IPAddress = args[2];
                ipConfig.SubnetMask = args[3];
                ipConfig.DefaultGateway = args[4];
            }

            bool isAutoDNS = bool.Parse(args[5]);
            dns = new DNS() { ObtainDNSAutomatically = isAutoDNS };
            if (dns.ObtainDNSAutomatically == false)
            {
                dns.DNSAddress1 = args[6];
                dns.DNSAddress2 = args[7];
            }

            bool useProxy = bool.Parse(args[8]);
            proxyConfig = new ProxyConfig() { UseProxy = useProxy };
            if (proxyConfig.UseProxy)
            {
                proxyConfig.ProxyAddress = args[9];
                proxyConfig.Port = int.Parse(args[10]);
                proxyConfig.ProxyOverride = args[11];
            }
        }

        internal static void ChangeConfig(string adapterName, IPConfig ipConfig, DNS dns, ProxyConfig proxyConfig)
        {
            NetworkAdapter adapter = factory.GetNetworkAdapter(adapterName);
            if (dns.ObtainDNSAutomatically)
            {
                adapter.EnableAutoDNS();
            }
            else
            {
                adapter.SetDns(new string[] { dns.DNSAddress1, dns.DNSAddress2 });
            }

            if (ipConfig.IsDHCPEnabled)
            {
                adapter.EnableDHCP();
            }
            else
            {
                adapter.SetIp(ipConfig.IPAddress, ipConfig.SubnetMask, ipConfig.DefaultGateway);
            }

            if (proxyConfig.UseProxy)
            {
                SystemProxyManagement.EnableProxy(proxyConfig.ProxyAddress, proxyConfig.Port, proxyConfig.ProxyOverride);
            }
            else
            {
                SystemProxyManagement.DisableProxy();
            }
        }

        internal static void ChangeConfig(string adapterName, NetworkAdapterConfiguration config)
        {
            NetworkAdapter adapter = factory.GetNetworkAdapter(adapterName);
            if (config.ObtainDNSAutomatically)
            {
                adapter.EnableAutoDNS();
            }
            else
            {
                adapter.SetDns(new string[] { config.DNS1, config.DNS2 });
            }

            if (config.IsDHCPEnabled)
            {
                adapter.EnableDHCP();
            }
            else
            {
                adapter.SetIp(config.IPAddress, config.SubnetMask, config.DefaultGateway);
            }

            if (config.UseProxy)
            {
                SystemProxyManagement.EnableProxy(config.ProxyAddress, config.ProxyPort, config.ProxyOverride);
            }
            else
            {
                SystemProxyManagement.DisableProxy();
            }
        }
    }
}
